﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using ScormModel.View;
using ScormModel;
using System.IO;
using System.Xml.Serialization;

namespace WpfELearningAuthoringTool
{
    /// <summary>
    /// Interaction logic for SequencingCourseConfiguration.xaml
    /// </summary>
    public partial class SequencingCourseConfiguration : Window
    {
        ObservableCollection<ConditionView> listPreConditionData = new ObservableCollection<ConditionView>();
        ObservableCollection<ConditionActionView> listPreConditionActionData = new ObservableCollection<ConditionActionView>();
        ObservableCollection<ConditionView> listPostConditionData = new ObservableCollection<ConditionView>();
        ObservableCollection<ConditionActionView> listPostConditionActionData = new ObservableCollection<ConditionActionView>();
        ObservableCollection<ConditionView> listExitConditionData = new ObservableCollection<ConditionView>();
        ObservableCollection<ConditionActionView> listExitConditionActionData = new ObservableCollection<ConditionActionView>();
        ObservableCollection<RollUpRuleView> listRollupConditionData = new ObservableCollection<RollUpRuleView>();
        ObservableCollection<ConditionActionView> listRollupConditionActionData = new ObservableCollection<ConditionActionView>();
        private Sequencing sequencingConfiguration;
        private Organization selectedItem;
        private ObservableCollection<Sequencing> listSequencing = new ObservableCollection<Sequencing>();
        private DataGrid sequencingGrid;
        private int index;
        private bool isAddNew = false;
        public Organization SelectedItem
        {
            get { return this.selectedItem; }
            set { this.selectedItem = value; }
        }
        public Sequencing SequencingInfo
        {
            get { return this.sequencingConfiguration; }
            set { this.sequencingConfiguration = value; }
        }
        public bool IsAddNew
        {
            get { return this.isAddNew; }
            set { this.isAddNew = value; }
        }
        public SequencingCourseConfiguration()
        {
            InitializeComponent();
            enableControlMode(false);
            enableLimitCondition(false);
            enableRandomizationControl(false);
            enableDeliveryControl(false);
            enableConstraintChoice(false);
            enableRollupConsideration(false);
            enablePreConditionRule(false);
            enablePostConditionRule(false);
            enableRollUpRule(false);
            enableExitConditionRule(false);
            listPreconditionGrid.ItemsSource = listPreConditionData;
            listPreConditionActionGrid.ItemsSource = listPreConditionActionData;
            listPostConditionActionGrid.ItemsSource = listPostConditionData;
            listPostConditionActionGrid.ItemsSource = listPostConditionActionData;
            listExitConditionActionGrid.ItemsSource = listExitConditionData;
            listExitConditionActionGrid.ItemsSource = listExitConditionActionData;
            listRollupRuleGrid.ItemsSource = listRollupConditionData;
            listRollupConditionActionGrid.ItemsSource = listRollupConditionActionData;
        }
        public SequencingCourseConfiguration(Organization selectedItem)
        {
            InitializeComponent();
            enableControlMode(false);
            enableLimitCondition(false);
            enableRandomizationControl(false);
            enableDeliveryControl(false);
            enableConstraintChoice(false);
            enableRollupConsideration(false);
            enablePreConditionRule(false);
            enablePostConditionRule(false);
            enableRollUpRule(false);
            enableExitConditionRule(false);
            this.selectedItem = selectedItem;
            if (this.selectedItem.Sequencing.ControlMode != null)
            {
                cbUseCM.IsChecked = true;
                enableControlMode(true);
                HanleLoadControlMode(this.selectedItem.Sequencing.ControlMode);
            }
            if (this.selectedItem.Sequencing.DeliveryControls != null)
            {
                cbUseDC.IsChecked = true;
                enableDeliveryControl(true);
                HandleLoadDeliveryControl(this.selectedItem.Sequencing.DeliveryControls);
            }
            if (this.selectedItem.Sequencing.LimitConditions != null)
            {
                cbUseLimit.IsChecked = true;
                enableLimitCondition(true);
                HandleLoadLimitCondition(this.selectedItem.Sequencing.LimitConditions);
            }
            if (this.selectedItem.Sequencing.ConstrainedChoiceConsiderations != null)
            {
                cbUseConstraintChoice.IsChecked = true;
                enableConstraintChoice(true);
                HandleConstraintChoice(this.selectedItem.Sequencing.ConstrainedChoiceConsiderations);
            }
            if (this.selectedItem.Sequencing.RandomizationControls != null)
            {
                cbUseRC.IsChecked = true;
                enableRandomizationControl(true);
                HandleLoadRandomizationControl(this.selectedItem.Sequencing.RandomizationControls);
            }
            if (this.selectedItem.Sequencing.RollupConsiderations != null)
            {
                cbUseRollupConsideration.IsChecked = true;
                enableRollupConsideration(true);
                HandleLoadRollupConsideration(this.selectedItem.Sequencing.RollupConsiderations);
            }
            if (this.selectedItem.Sequencing.ListPreConditionData.Count > 0)
            {
                usePreCond.IsChecked = true;
                enablePreConditionRule(true);
                HandleLoadPreCoditionRule(this.selectedItem.Sequencing.ListPreConditionData, this.selectedItem.Sequencing.ListPreConditionActionData);

            }
            if (this.selectedItem.Sequencing.ListPostConditionData.Count > 0)
            {
                usePostCond.IsChecked = true;
                enablePostConditionRule(true);
                this.listPostConditionData = this.selectedItem.Sequencing.ListPostConditionData;
                //   listPostConditionActionGrid.ItemsSource = listPostConditionData;
                HandleLoadPostConditionRule(this.selectedItem.Sequencing.ListPostConditionData, this.selectedItem.Sequencing.ListPostConditionActionData);

            }
            if (this.selectedItem.Sequencing.ListExitConditionData.Count > 0)
            {
                useExitCond.IsChecked = true;
                enableExitConditionRule(true);
                HandleLoadExitConditionRule(this.selectedItem.Sequencing.ListExitConditionData, this.selectedItem.Sequencing.ListExitConditionActionData);
                //   this.listExitConditionData = this.selectedItem.Sequencing.ListExitConditionData;
            }
            if (this.selectedItem.Sequencing.ListRollupConditionData.Count > 0 || this.selectedItem.Sequencing.RollupRules != null)
            {
                useRollupRule.IsChecked = true;
                enableRollUpRule(true);
                if (this.selectedItem.Sequencing.RollupRules.RollupObjectiveSatisfied != null)
                {
                    useRollupObjSatisfied.IsChecked = true;
                    cbRollupObjSatisfied.SelectedValue = this.selectedItem.Sequencing.RollupRules.RollupObjectiveSatisfied;
                }
                if (this.selectedItem.Sequencing.RollupRules.RollupProgressCompletion != null)
                {
                    useRollupProgressComplete.IsChecked = true;
                    cbRollupProgressComplete.SelectedValue = this.selectedItem.Sequencing.RollupRules.RollupProgressCompletion;
                }
                if (this.selectedItem.Sequencing.RollupRules.ObjectiveMeasureWeight != null)
                {
                    useObjMeasureWeight.IsChecked = true;
                    txtObjMeasureWeight.Text = this.selectedItem.Sequencing.RollupRules.ObjectiveMeasureWeight;
                }
                HandleLoadRollupCondition(this.selectedItem.Sequencing.ListRollupConditionData, this.selectedItem.Sequencing.ListRollupConditionActionData);
                //    this.listRollupConditionData = this.selectedItem.Sequencing.ListRollupConditionData;
            }
        }
        public SequencingCourseConfiguration(Sequencing sequencingConfiguration, ObservableCollection<Sequencing> listSequencing, int index, DataGrid dataGrid)
        {
            InitializeComponent();
            this.sequencingConfiguration = sequencingConfiguration;
            this.listSequencing = listSequencing;
            this.index = index;
            this.sequencingGrid = dataGrid;
            if (sequencingConfiguration.IsEdited == false)
            {
                enableControlMode(false);
                enableLimitCondition(false);
                enableRandomizationControl(false);
                enableDeliveryControl(false);
                enableConstraintChoice(false);
                enableRollupConsideration(false);
                enablePreConditionRule(false);
                enablePostConditionRule(false);
                enableRollUpRule(false);
                enableExitConditionRule(false);
                listPreconditionGrid.ItemsSource = sequencingConfiguration.ListPreConditionData;
                listPreConditionActionGrid.ItemsSource = sequencingConfiguration.ListPreConditionActionData;

                listPostConditionGrid.ItemsSource = sequencingConfiguration.ListPostConditionData;
                listPostConditionActionGrid.ItemsSource = sequencingConfiguration.ListPostConditionActionData;

                listExitConditionGrid.ItemsSource = sequencingConfiguration.ListExitConditionData;
                listExitConditionActionGrid.ItemsSource = sequencingConfiguration.ListExitConditionActionData;

                listRollupRuleGrid.ItemsSource = sequencingConfiguration.ListRollupConditionData;
                listRollupConditionActionGrid.ItemsSource = sequencingConfiguration.ListRollupConditionActionData;
            }
            else
            {
                if (sequencingConfiguration.ControlMode != null)
                {
                    cbUseCM.IsChecked = true;
                    HanleLoadControlMode(sequencingConfiguration.ControlMode);
                }
                if (sequencingConfiguration.DeliveryControls != null)
                {
                    cbUseDC.IsChecked = true;
                    HandleLoadDeliveryControl(sequencingConfiguration.DeliveryControls);
                }
                if (sequencingConfiguration.LimitConditions != null)
                {
                    cbUseLimit.IsChecked = true;
                    HandleLoadLimitCondition(sequencingConfiguration.LimitConditions);
                }
                if (sequencingConfiguration.ConstrainedChoiceConsiderations != null)
                {
                    cbUseConstraintChoice.IsChecked = true;
                    HandleConstraintChoice(sequencingConfiguration.ConstrainedChoiceConsiderations);
                }
                if (sequencingConfiguration.RandomizationControls != null)
                {
                    cbUseRC.IsChecked = true;
                    HandleLoadRandomizationControl(sequencingConfiguration.RandomizationControls);
                }
                if (sequencingConfiguration.RollupConsiderations != null)
                {
                    cbUseRollupConsideration.IsChecked = true;
                    HandleLoadRollupConsideration(sequencingConfiguration.RollupConsiderations);
                }
                if (sequencingConfiguration.ListPreConditionData.Count > 0)
                {
                    usePreCond.IsChecked = true;
                    HandleLoadPreCoditionRule(sequencingConfiguration.ListPreConditionData, sequencingConfiguration.ListPreConditionActionData);
                    //  this.listPreConditionData = sequencingConfiguration.ListPreConditionData;
                }
                if (sequencingConfiguration.ListPostConditionData.Count > 0)
                {
                    usePostCond.IsChecked = true;
                    HandleLoadPostConditionRule(sequencingConfiguration.ListPostConditionData, sequencingConfiguration.ListPostConditionActionData);
                    //   this.listPostConditionData = sequencingConfiguration.ListPostConditionData;
                }
                if (sequencingConfiguration.ListExitConditionData.Count > 0)
                {
                    useExitCond.IsChecked = true;
                    HandleLoadExitConditionRule(sequencingConfiguration.ListExitConditionData, sequencingConfiguration.ListExitConditionActionData);
                    //   this.listExitConditionData = sequencingConfiguration.ListExitConditionData;
                }
                if (sequencingConfiguration.ListRollupConditionData.Count > 0)
                {
                    useRollupRule.IsChecked = true;
                    HandleLoadRollupCondition(sequencingConfiguration.ListRollupConditionData, sequencingConfiguration.ListRollupConditionActionData);
                    //   this.listRollupConditionData = sequencingConfiguration.ListRollupConditionData;
                }
            }
        }


        #region Precondition Section
        private void btnAddPre_Click(object sender, RoutedEventArgs e)
        {
            ConditionView pcv = new ConditionView();
            pcv.Id = (listPreConditionData.Count + 1).ToString();
            pcv.Description = "Pre Condition " + (listPreConditionData.Count + 1).ToString();
            pcv.ConditionMode = "any";
            listPreConditionData.Add(pcv);
            listPreconditionGrid.ItemsSource = listPreConditionData;
        }

        private void btnRemovePre_Click(object sender, RoutedEventArgs e)
        {
            if (listPreconditionGrid.SelectedIndex >= 0)
            {
                listPreConditionData.RemoveAt(listPreconditionGrid.SelectedIndex);
                List<ConditionView> lpv = new List<ConditionView>();
                for (int i = 0; i < listPreConditionData.Count; i++)
                {
                    ConditionView qi = listPreConditionData[i];
                    qi.Id = (i + 1).ToString();
                    lpv.Add(qi);
                }
                listPreConditionData.Clear();
                for (int i = 0; i < lpv.Count; i++)
                {
                    listPreConditionData.Add(lpv[i]);
                }
                listPreConditionActionData = new ObservableCollection<ConditionActionView>();
                listPreConditionActionGrid.ItemsSource = listPreConditionActionData;
            }
        }


        private void btnAddPreConAct_Click(object sender, RoutedEventArgs e)
        {
            if (listPreconditionGrid.SelectedIndex >= 0)
            {
                ConditionActionView cav = new ConditionActionView();
                cav.Id = (listPreConditionActionData.Count + 1).ToString();
                cav.Condition = ((ComboBoxItem)(cbListPreCondition.SelectedItem)).Content.ToString();
                //   cav.Action = ((ComboBoxItem)(cbPreAction.SelectedItem)).Content.ToString();
                cav.Operator = ((ComboBoxItem)(cbPreOperator.SelectedItem)).Content.ToString();
                bool duplicate = false;
                for (int i = 0; i < listPreConditionActionData.Count; i++)
                {
                    if (listPreConditionActionData[i].Condition == cav.Condition && listPreConditionActionData[i].Action == cav.Action)
                    {
                        duplicate = true;
                        break;
                    }
                    else
                        continue;
                }
                if (duplicate == false)
                {
                    listPreConditionActionData.Add(cav);
                    listPreConditionData[listPreconditionGrid.SelectedIndex].ListConditionAction = listPreConditionActionData;
                    listPreConditionActionGrid.ItemsSource = listPreConditionActionData;
                }
                else
                {
                    MessageBox.Show("Duplicate Condition");
                }
            }
            else
            {
                MessageBox.Show("Please select a Precondition");
            }
        }

        private void btnRemovePreConAct_Click(object sender, RoutedEventArgs e)
        {
            if (listPreConditionActionGrid.SelectedIndex > 0)
            {
                listPreConditionActionData.RemoveAt(listPreConditionActionGrid.SelectedIndex);
                List<ConditionActionView> lca = new List<ConditionActionView>();
                for (int i = 0; i < listPreConditionActionData.Count; i++)
                {
                    ConditionActionView cav = listPreConditionActionData[i];
                    cav.Id = (i + 1).ToString();
                    lca.Add(cav);
                }
                listPreConditionActionData.Clear();
                for (int i = 0; i < lca.Count; i++)
                {
                    listPreConditionActionData.Add(lca[i]);
                }
                listPreConditionActionGrid.ItemsSource = listPreConditionActionData;
            }
        }
        #endregion

        #region Postcondition Section
        private void btnAddPost_Click(object sender, RoutedEventArgs e)
        {
            ConditionView pcv = new ConditionView();
            pcv.Id = (listPostConditionData.Count + 1).ToString();
            pcv.Description = "Post Condition " + (listPostConditionData.Count + 1).ToString();
            pcv.ConditionMode = "all";
            listPostConditionData.Add(pcv);
            listPostConditionGrid.ItemsSource = listPostConditionData;
        }

        private void btnRemovePost_Click(object sender, RoutedEventArgs e)
        {
            if (listPostConditionGrid.SelectedIndex >= 0)
            {
                listPostConditionData.RemoveAt(listPostConditionGrid.SelectedIndex);
                List<ConditionView> lpv = new List<ConditionView>();
                for (int i = 0; i < listPostConditionData.Count; i++)
                {
                    ConditionView qi = listPostConditionData[i];
                    qi.Id = (i + 1).ToString();
                    lpv.Add(qi);
                }
                listPostConditionData.Clear();
                for (int i = 0; i < lpv.Count; i++)
                {
                    listPostConditionData.Add(lpv[i]);
                }
                listPostConditionActionData = new ObservableCollection<ConditionActionView>();
                listPostConditionActionGrid.ItemsSource = listPostConditionActionData;
                listPostConditionGrid.ItemsSource = listPostConditionData;
            }
        }

        private void btnAddPostConAct_Click(object sender, RoutedEventArgs e)
        {
            if (listPostConditionGrid.SelectedIndex >= 0)
            {
                ConditionActionView cav = new ConditionActionView();
                cav.Id = (listPostConditionActionData.Count + 1).ToString();
                cav.Condition = ((ComboBoxItem)(cbListPostCondition.SelectedItem)).Content.ToString();
                //cav.Action = ((ComboBoxItem)(cbPostAction.SelectedItem)).Content.ToString();
                cav.Operator = ((ComboBoxItem)(cbPostOperator.SelectedItem)).Content.ToString();
                bool duplicate = false;
                for (int i = 0; i < listPostConditionActionData.Count; i++)
                {
                    if (listPostConditionActionData[i].Condition == cav.Condition && listPostConditionActionData[i].Action == cav.Action)
                    {
                        duplicate = true;
                        break;
                    }
                    else
                        continue;
                }
                if (duplicate == false)
                {
                    listPostConditionActionData.Add(cav);
                    listPostConditionData[listPostConditionGrid.SelectedIndex].ListConditionAction = listPostConditionActionData;
                    listPostConditionActionGrid.ItemsSource = listPostConditionActionData;
                }
                else
                {
                    MessageBox.Show("Duplicate Condition");
                }
            }
            else
            {
                MessageBox.Show("Please select a Post Condition");
            }
        }

        private void btnRemovePostConAct_Click(object sender, RoutedEventArgs e)
        {
            if (listPostConditionActionGrid.SelectedIndex >= 0)
            {
                listPostConditionActionData.RemoveAt(listPostConditionActionGrid.SelectedIndex);
                List<ConditionActionView> lca = new List<ConditionActionView>();
                for (int i = 0; i < listPostConditionActionData.Count; i++)
                {
                    ConditionActionView cav = listPostConditionActionData[i];
                    cav.Id = (i + 1).ToString();
                    lca.Add(cav);
                }
                listPostConditionActionData.Clear();
                for (int i = 0; i < lca.Count; i++)
                {
                    listPostConditionActionData.Add(lca[i]);
                }
                listPostConditionActionGrid.ItemsSource = listPostConditionActionData;
            }
        }
        #endregion

        #region Exitcondition Section
        private void btnAddExit_Click(object sender, RoutedEventArgs e)
        {
            ConditionView pcv = new ConditionView();
            pcv.Id = (listExitConditionData.Count + 1).ToString();
            pcv.Description = "Exit Condition " + (listExitConditionData.Count + 1).ToString();
            pcv.ConditionMode = "all";
            listExitConditionData.Add(pcv);
            listExitConditionGrid.ItemsSource = listExitConditionData;
        }

        private void btnRemoveExit_Click(object sender, RoutedEventArgs e)
        {
            if (listExitConditionGrid.SelectedIndex >= 0)
            {
                listExitConditionData.RemoveAt(listExitConditionGrid.SelectedIndex);
                List<ConditionView> lpv = new List<ConditionView>();
                for (int i = 0; i < listExitConditionData.Count; i++)
                {
                    ConditionView qi = listExitConditionData[i];
                    qi.Id = (i + 1).ToString();
                    lpv.Add(qi);
                }
                listExitConditionData.Clear();
                for (int i = 0; i < lpv.Count; i++)
                {
                    listExitConditionData.Add(lpv[i]);
                }
                listExitConditionActionData = new ObservableCollection<ConditionActionView>();
                listExitConditionActionGrid.ItemsSource = listExitConditionActionData;
                listExitConditionGrid.ItemsSource = listExitConditionData;
            }
        }

        private void btnAddExitConAct_Click(object sender, RoutedEventArgs e)
        {
            if (listExitConditionGrid.SelectedIndex >= 0)
            {
                ConditionActionView cav = new ConditionActionView();
                cav.Id = (listExitConditionActionData.Count + 1).ToString();
                cav.Condition = ((ComboBoxItem)(cbListExitCondition.SelectedItem)).Content.ToString();
                //cav.Action = ((ComboBoxItem)(cbExitAction.SelectedItem)).Content.ToString();
                cav.Operator = ((ComboBoxItem)(cbExitOperator.SelectedItem)).Content.ToString();
                bool duplicate = false;
                for (int i = 0; i < listExitConditionActionData.Count; i++)
                {
                    if (listExitConditionActionData[i].Condition == cav.Condition /*&& listExitConditionActionData[i].Action == cav.Action*/)
                    {
                        duplicate = true;
                        break;
                    }
                    else
                        continue;
                }
                if (duplicate == false)
                {
                    listExitConditionActionData.Add(cav);
                    listExitConditionData[listExitConditionGrid.SelectedIndex].ListConditionAction = listExitConditionActionData;
                    listExitConditionActionGrid.ItemsSource = listExitConditionActionData;
                }
                else
                {
                    MessageBox.Show("Duplicate Condition");
                }
            }
            else
            {
                MessageBox.Show("Please select a Exit Condition");
            }
        }

        private void btnRemovExitConAct_Click(object sender, RoutedEventArgs e)
        {
            if (listExitConditionActionGrid.SelectedIndex >= 0)
            {
                listExitConditionActionData.RemoveAt(listExitConditionActionGrid.SelectedIndex);
                List<ConditionActionView> lca = new List<ConditionActionView>();
                for (int i = 0; i < listExitConditionActionData.Count; i++)
                {
                    ConditionActionView cav = listExitConditionActionData[i];
                    cav.Id = (i + 1).ToString();
                    lca.Add(cav);
                }
                listExitConditionActionData.Clear();
                for (int i = 0; i < lca.Count; i++)
                {
                    listExitConditionActionData.Add(lca[i]);
                }
                listExitConditionActionGrid.ItemsSource = listExitConditionActionData;
            }
        }
        #endregion
        #region Rollupcondition Section
        private void cbChildActivitySet_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbChildActivitySet.SelectedIndex > 0)
            {
                if (cbChildActivitySet.SelectedIndex == 3)
                {
                    txtMinimumCount.IsEnabled = true;
                    txtMinimumPercent.IsEnabled = false;
                }
                else if (cbChildActivitySet.SelectedIndex == 4)
                {
                    txtMinimumPercent.IsEnabled = true;
                    txtMinimumCount.IsEnabled = false;
                }
                else
                {
                    txtMinimumPercent.IsEnabled = false;
                    txtMinimumCount.IsEnabled = false;
                }
            }


        }
        private void btnAddRollup_Click(object sender, RoutedEventArgs e)
        {
            if (useChildActivitySet.IsChecked == true)
            {
                if (cbChildActivitySet.SelectedIndex == 3)
                {
                    if (txtMinimumCount.Text.Length == 0)
                    {
                        MessageBox.Show("Please input value for Minimum Count");
                        return;
                    }
                    RollUpRuleView rrv = new RollUpRuleView();
                    rrv.Id = (listRollupConditionData.Count + 1).ToString();
                    rrv.Description = "Rollup Rule " + (listRollupConditionData.Count + 1).ToString();
                    rrv.ChildActivitySet = ((ComboBoxItem)cbChildActivitySet.SelectedItem).Content.ToString();
                    rrv.MinimumCount = txtMinimumCount.Text;
                    rrv.ConditionMode = ((ComboBoxItem)cbConditionMode.SelectedItem).Content.ToString();
                    listRollupConditionData.Add(rrv);
                }
                else if (cbChildActivitySet.SelectedIndex == 4)
                {
                    if (txtMinimumPercent.Text.Length == 0)
                    {
                        MessageBox.Show("Please input value for Minimum Percent");
                        return;
                    }
                    RollUpRuleView rrv = new RollUpRuleView();
                    rrv.Id = (listRollupConditionData.Count + 1).ToString();
                    rrv.Description = "Rollup Rule " + (listRollupConditionData.Count + 1).ToString();
                    rrv.ChildActivitySet = ((ComboBoxItem)cbChildActivitySet.SelectedItem).Content.ToString();
                    rrv.MinimumCount = txtMinimumPercent.Text;
                    rrv.ConditionMode = ((ComboBoxItem)cbConditionMode.SelectedItem).Content.ToString();
                    listRollupConditionData.Add(rrv);
                }
                else
                {
                    RollUpRuleView rrv = new RollUpRuleView();
                    rrv.Id = (listRollupConditionData.Count + 1).ToString();
                    rrv.Description = "Rollup Rule " + (listRollupConditionData.Count + 1).ToString();
                    rrv.ChildActivitySet = ((ComboBoxItem)cbChildActivitySet.SelectedItem).Content.ToString();
                    rrv.ConditionMode = ((ComboBoxItem)cbConditionMode.SelectedItem).Content.ToString();
                    listRollupConditionData.Add(rrv);
                }
            }
            else
            {
                RollUpRuleView rrv = new RollUpRuleView();
                rrv.Id = (listRollupConditionData.Count + 1).ToString();
                rrv.Description = "Rollup Rule " + (listRollupConditionData.Count + 1).ToString();
                rrv.ConditionMode = ((ComboBoxItem)cbConditionMode.SelectedItem).Content.ToString();
                listRollupConditionData.Add(rrv);
            }
            listRollupRuleGrid.ItemsSource = listRollupConditionData;
        }

        private void btnRemoveRollup_Click(object sender, RoutedEventArgs e)
        {
            if (listRollupRuleGrid.SelectedIndex >= 0)
            {
                listRollupConditionData.RemoveAt(listRollupRuleGrid.SelectedIndex);
                List<RollUpRuleView> lpv = new List<RollUpRuleView>();
                for (int i = 0; i < listRollupConditionData.Count; i++)
                {
                    RollUpRuleView qi = listRollupConditionData[i];
                    qi.Id = (i + 1).ToString();
                    lpv.Add(qi);
                }
                listRollupConditionData.Clear();
                for (int i = 0; i < lpv.Count; i++)
                {
                    listRollupConditionData.Add(lpv[i]);
                }
                listRollupConditionActionData = new ObservableCollection<ConditionActionView>();
                listRollupConditionActionGrid.ItemsSource = listRollupConditionActionData;
                listRollupRuleGrid.ItemsSource = listRollupConditionData;
            }
        }

        private void btnAddRollupConAct_Click(object sender, RoutedEventArgs e)
        {
            if (listRollupRuleGrid.SelectedIndex >= 0)
            {
                ConditionActionView cav = new ConditionActionView();
                cav.Id = (listRollupConditionActionData.Count + 1).ToString();
                cav.Condition = ((ComboBoxItem)(cbListRollupCondition.SelectedItem)).Content.ToString();
                cav.Operator = ((ComboBoxItem)(cbRollupOperator.SelectedItem)).Content.ToString();
                bool duplicate = false;
                for (int i = 0; i < listRollupConditionActionData.Count; i++)
                {
                    if (listRollupConditionActionData[i].Condition == cav.Condition)
                    {
                        duplicate = true;
                        break;
                    }
                    else
                        continue;
                }
                if (duplicate == false)
                {
                    listRollupConditionActionData.Add(cav);
                    listRollupConditionData[listRollupRuleGrid.SelectedIndex].ListConditionAction = listRollupConditionActionData;
                    listRollupConditionActionGrid.ItemsSource = listRollupConditionActionData;
                }
                else
                {
                    MessageBox.Show("Duplicate Condition");
                }
            }
            else
            {
                MessageBox.Show("Please select a Rollup Rule");
            }
        }

        private void btnRemoveRollupConAct_Click(object sender, RoutedEventArgs e)
        {
            if (listRollupConditionActionGrid.SelectedIndex >= 0)
            {
                listRollupConditionActionData.RemoveAt(listRollupConditionActionGrid.SelectedIndex);
                List<ConditionActionView> lca = new List<ConditionActionView>();
                for (int i = 0; i < listRollupConditionActionData.Count; i++)
                {
                    ConditionActionView cav = listRollupConditionActionData[i];
                    cav.Id = (i + 1).ToString();
                    lca.Add(cav);
                }
                listRollupConditionActionData.Clear();
                for (int i = 0; i < lca.Count; i++)
                {
                    listRollupConditionActionData.Add(lca[i]);
                }
                listRollupConditionActionGrid.ItemsSource = listRollupConditionActionData;
            }
        }
        #endregion

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            //Sequencing sequencing = new Sequencing();
            //XmlSerializer serializer = new XmlSerializer(typeof(Sequencing));
            if (sequencingConfiguration != null && sequencingConfiguration.IsEdited == true)
            {
                string id = sequencingConfiguration.ID;
                string name = sequencingConfiguration.SequencingName;
                string des = sequencingConfiguration.Description;
                sequencingConfiguration = new Sequencing();
                sequencingConfiguration.ID = id;
                sequencingConfiguration.SequencingName = name;
                sequencingConfiguration.Description = des;
                listSequencing[index] = sequencingConfiguration;
            }
            if (sequencingConfiguration != null)
            {
                sequencingConfiguration.IsEdited = true;
            }
            if (IsAddNew == true)
            {
                sequencingConfiguration = new Sequencing();
            }

            #region Handle Sequencing Control
            if (cbUseCM.IsChecked == true)
            {
                ControlMode cm = handleControlMode();
                sequencingConfiguration.ControlMode = cm;
            }
            if (cbUseLimit.IsChecked == true)
            {
                LimitConditions lm = hanleLimitConditions();
                sequencingConfiguration.LimitConditions = lm;
            }
            if (cbUseRC.IsChecked == true)
            {
                RandomizationControls rc = handleRandomizationControl();
                sequencingConfiguration.RandomizationControls = rc;
            }
            if (cbUseDC.IsChecked == true)
            {
                DeliveryControls dc = handleDeliveryControl();
                sequencingConfiguration.DeliveryControls = dc;
            }
            if (cbUseConstraintChoice.IsChecked == true)
            {
                ConstrainedChoiceConsiderations ccc = handleConstrainedChoiceConsiderations();
                sequencingConfiguration.ConstrainedChoiceConsiderations = ccc;
            }
            if (cbUseRollupConsideration.IsChecked == true)
            {
                RollupConsiderations rc = handleRollupConsideration();
                sequencingConfiguration.RollupConsiderations = rc;
            }
            #endregion
            #region Handle SequencingRules
            if (usePreCond.IsChecked == true || usePostCond.IsChecked == true || useExitCond.IsChecked == true)
            {
                SequencingRules sequencingRules = new SequencingRules();
                if (usePreCond.IsChecked == true)
                {
                    List<PreConditionRule> listPreConditonRule = handlePreConditionRule();
                    sequencingConfiguration.ListPreConditionData = listPreConditionData;
                    sequencingRules.ListPreConditionRule = listPreConditonRule;
                }
                if (usePostCond.IsChecked == true)
                {
                    List<PostConditionRule> listPostConditionRule = handlePostConditionRule();
                    sequencingConfiguration.ListPostConditionData = listPostConditionData;
                    sequencingRules.ListPostConditionRule = listPostConditionRule;
                }
                if (useExitCond.IsChecked == true)
                {
                    List<ExitConditionRule> listExitConditionRule = handleExitConditionRule();
                    sequencingConfiguration.ListExitConditionData = listExitConditionData;
                    sequencingRules.ListExitConditionRule = listExitConditionRule;
                }
                sequencingConfiguration.SequencingRules = sequencingRules;
            }
            #endregion
            #region Handle Rollup Rules
            if (useRollupRule.IsChecked == true)
            {
                RollupRules rrs = new RollupRules();
                if (useRollupObjSatisfied.IsChecked == true)
                {
                    rrs.RollupObjectiveSatisfied = ((ComboBoxItem)(cbRollupObjSatisfied.SelectedItem)).Content.ToString();
                }
                if (useRollupProgressComplete.IsChecked == true)
                {
                    rrs.RollupProgressCompletion = ((ComboBoxItem)(cbRollupProgressComplete.SelectedItem)).Content.ToString();
                }
                if (useObjMeasureWeight.IsChecked == true)
                {
                    rrs.ObjectiveMeasureWeight = txtObjMeasureWeight.Text;
                }
                rrs.ListRollupRule = handleRollupRule();
                sequencingConfiguration.ListRollupConditionData = listRollupConditionData;
                sequencingConfiguration.RollupRules = rrs;
            }
            #endregion
            if (IsAddNew == false)
            {
                sequencingGrid.ItemsSource = listSequencing;
            }
            else
            {
                ////add tree view selected item
                //Objectives obj = null;
                //if (selectedItem.ItemType == "MODULE")
                //{
                //    if (selectedItem.Sequencing != null)
                //        obj = selectedItem.Sequencing.Objectives;
                //}
                selectedItem.Sequencing = sequencingConfiguration;
                //if (obj != null)
                //    selectedItem.Sequencing.Objectives = obj;
            }
            Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        #region Handle Load Sequencing
        private void HanleLoadControlMode(ControlMode controlMode)
        {
            if (controlMode.Choice != null && controlMode.ChoiceExit != null && controlMode.Flow != null && controlMode.ForwardOnly != null)
            {
                if (controlMode.Choice == "true" && controlMode.ChoiceExit == "true" && controlMode.Flow == "false" && controlMode.ForwardOnly == "false")
                {
                    cbUseCmDf.IsChecked = true;
                }
                else
                {
                    cbUseCmCus.IsChecked = true;
                    cbUseChoice.IsChecked = true;
                    cbUseChoiceExit.IsChecked = true;
                    cbUseFlow.IsChecked = true;
                    cbUseForward.IsChecked = true;
                    cbChoice.SelectedValue = controlMode.Choice;
                    cbChoiceExit.SelectedValue = controlMode.ChoiceExit;
                    cbFlow.SelectedValue = controlMode.Flow;
                    cbForward.SelectedValue = controlMode.ForwardOnly;
                }
            }
            else
            {
                if (controlMode.Choice != null || controlMode.ChoiceExit != null || controlMode.Flow != null || controlMode.ForwardOnly != null)
                {
                    cbUseCmCus.IsChecked = true;
                    if (controlMode.Choice != null)
                    {
                        cbUseChoice.IsChecked = true;
                        cbChoice.SelectedValue = controlMode.Choice;
                    }
                    if (controlMode.ChoiceExit != null)
                    {
                        cbUseChoiceExit.IsChecked = true;
                        cbChoiceExit.SelectedValue = controlMode.ChoiceExit;
                    }
                    if (controlMode.Flow != null)
                    {
                        cbUseFlow.IsChecked = true;
                        cbFlow.SelectedValue = controlMode.Flow;
                    }
                    if (controlMode.ForwardOnly != null)
                    {
                        cbUseForward.IsChecked = true;
                        cbForward.SelectedValue = controlMode.ForwardOnly;
                    }
                }
            }
        }
        private void HandleLoadDeliveryControl(DeliveryControls deliveryControls)
        {
            if (deliveryControls.Tracked != null)
            {
                cbUseTrack.IsChecked = true;
                cbTrack.SelectedValue = deliveryControls.Tracked;
            }
            if (deliveryControls.CompletionSetByContent != null)
            {
                cbCompleteByContent.IsChecked = true;
                cbCBC.SelectedValue = deliveryControls.CompletionSetByContent;
            }
            if (deliveryControls.ObjectiveSetByContent != null)
            {
                cbObjectiveByContent.IsChecked = true;
                cbOBC.SelectedValue = deliveryControls.ObjectiveSetByContent;
            }
        }
        private void HandleLoadLimitCondition(LimitConditions limitConditions)
        {
            if (limitConditions.AttemptLimit != null)
            {
                cbAttemptLimit.IsChecked = true;
                txtAttemptLimit.Text = limitConditions.AttemptLimit;
            }
            if (limitConditions.AttemptAbsoluteDurationLimit != null)
            {
                cbAttemptDuration.IsChecked = true;
                txtAttemptDuration.Text = limitConditions.AttemptAbsoluteDurationLimit;
            }
        }
        private void HandleConstraintChoice(ConstrainedChoiceConsiderations constraintChoice)
        {
            if (constraintChoice.ConstrainChoice != null)
            {
                cbConstraint.IsChecked = true;
                cbConstraintChoice.SelectedValue = constraintChoice.ConstrainChoice;
            }
            if (constraintChoice.PreventActivation != null)
            {
                cbPrevent.IsChecked = true;
                cbPreventActivation.SelectedValue = constraintChoice.PreventActivation;
            }
        }
        private void HandleLoadRandomizationControl(RandomizationControls randomizationControls)
        {
            if (randomizationControls.RandomizationTiming != null)
            {
                cbRandomTiming.IsChecked = true;
                cbRandomizationTiming.SelectedValue = randomizationControls.RandomizationTiming;
            }
            if (randomizationControls.ReorderChildren != null)
            {
                cbReorder.IsChecked = true;
                cbReorderChildren.SelectedValue = randomizationControls.ReorderChildren;
            }
            if (randomizationControls.SelectCount != null)
            {
                cbSelectCount.IsChecked = true;
                txtSelectCount.Text = randomizationControls.SelectCount;
            }
            if (randomizationControls.SelectionTiming != null)
            {
                cbSelectTiming.IsChecked = true;
                cbSelectionTiming.SelectedValue = randomizationControls.SelectionTiming;
            }
        }
        private void HandleLoadRollupConsideration(RollupConsiderations rollupConsiderations)
        {
            if (rollupConsiderations.RequiredForSatisfied != null)
            {
                cbRFS.IsChecked = true;
                cbRequireForSatisfied.SelectedValue = rollupConsiderations.RequiredForSatisfied;
            }
            if (rollupConsiderations.RequiredForNotSatisfied != null)
            {
                cbRFNS.IsChecked = true;
                cbRequireForNotSatisfied.SelectedValue = rollupConsiderations.RequiredForNotSatisfied;
            }
            if (rollupConsiderations.RequiredForIncomplete != null)
            {
                cbRFIC.IsChecked = true;
                cbRequireForComplete.SelectedValue = rollupConsiderations.RequiredForIncomplete;
            }
            if (rollupConsiderations.RequiredForCompleted != null)
            {
                cbRFC.IsChecked = true;
                cbRequireForIncomplete.SelectedValue = rollupConsiderations.RequiredForCompleted;
            }
            if (rollupConsiderations.MeasureSatisfactionIfActive != null)
            {
                cbMSIA.IsChecked = true;
                cbMeasureSatisfationIfActive.SelectedValue = rollupConsiderations.MeasureSatisfactionIfActive;
            }
        }
        private void HandleLoadPreCoditionRule(ObservableCollection<ConditionView> listPreConditionData, ObservableCollection<ConditionActionView> listPreConditionActionData)
        {
            this.listPreConditionData = listPreConditionData;
            listPreconditionGrid.ItemsSource = this.listPreConditionData;
            listPreConditionActionGrid.ItemsSource = listPreConditionActionData;
        }
        private void HandleLoadPostConditionRule(ObservableCollection<ConditionView> listPostConditionData, ObservableCollection<ConditionActionView> listPostConditionActionData)
        {
            this.listPostConditionData = listPostConditionData;
            listPostConditionGrid.ItemsSource = listPostConditionData;
            listPostConditionActionGrid.ItemsSource = listPostConditionActionData;
        }
        private void HandleLoadExitConditionRule(ObservableCollection<ConditionView> listExitConditionData, ObservableCollection<ConditionActionView> listExitConditionActionData)
        {
            this.listExitConditionData = listExitConditionData;
            listExitConditionGrid.ItemsSource = this.listExitConditionData;
            listExitConditionActionGrid.ItemsSource = listExitConditionActionData;
        }
        private void HandleLoadRollupCondition(ObservableCollection<RollUpRuleView> listRollupConditionData, ObservableCollection<ConditionActionView> listRollupConditionActionData)
        {
            this.listRollupConditionData = listRollupConditionData;
            listRollupRuleGrid.ItemsSource = this.listRollupConditionData;
            listRollupConditionActionGrid.ItemsSource = listRollupConditionActionData;
        }
        #endregion

        #region Handle Sequencing Control
        public ControlMode handleControlMode()
        {
            ControlMode controlMode = new ControlMode();
            if (cbUseCmDf.IsChecked == true)
            {
                controlMode.Choice = "true";
                controlMode.ChoiceExit = "true";
                controlMode.Flow = "false";
                controlMode.ForwardOnly = "false";
            }
            else if (cbUseCmCus.IsChecked == true)
            {
                if (cbUseChoice.IsChecked == true)
                {
                    controlMode.Choice = ((ComboBoxItem)cbChoice.SelectedItem).Content.ToString();
                }
                if (cbUseChoiceExit.IsChecked == true)
                {
                    controlMode.ChoiceExit = ((ComboBoxItem)cbChoiceExit.SelectedItem).Content.ToString();
                }
                if (cbUseFlow.IsChecked == true)
                {
                    controlMode.Flow = ((ComboBoxItem)cbFlow.SelectedItem).Content.ToString();
                }
                if (cbUseForward.IsChecked == true)
                {
                    controlMode.ForwardOnly = ((ComboBoxItem)cbForward.SelectedItem).Content.ToString();
                }
            }
            return controlMode;
        }

        public LimitConditions hanleLimitConditions()
        {
            LimitConditions limitConditions = new LimitConditions();
            if (cbAttemptLimit.IsChecked == true)
            {
                limitConditions.AttemptLimit = txtAttemptLimit.Text;
            }
            if (cbAttemptDuration.IsChecked == true)
            {
                limitConditions.AttemptAbsoluteDurationLimit = txtAttemptDuration.Text;
            }
            return limitConditions;
        }

        public RandomizationControls handleRandomizationControl()
        {
            RandomizationControls rc = new RandomizationControls();
            if (cbRandomTiming.IsChecked == true)
            {
                rc.RandomizationTiming = ((ComboBoxItem)cbRandomizationTiming.SelectedItem).Content.ToString();
            }
            if (cbSelectCount.IsChecked == true)
            {
                rc.SelectCount = txtSelectCount.Text;
            }
            if (cbReorder.IsChecked == true)
            {
                rc.ReorderChildren = ((ComboBoxItem)cbReorderChildren.SelectedItem).Content.ToString();
            }
            if (cbSelectTiming.IsChecked == true)
            {
                rc.SelectionTiming = ((ComboBoxItem)cbSelectionTiming.SelectedItem).Content.ToString();
            }
            return rc;
        }

        public DeliveryControls handleDeliveryControl()
        {
            DeliveryControls dc = new DeliveryControls();
            if (cbUseTrack.IsChecked == true)
            {
                dc.Tracked = ((ComboBoxItem)cbTrack.SelectedItem).Content.ToString();
            }
            if (cbCompleteByContent.IsChecked == true)
            {
                dc.CompletionSetByContent = ((ComboBoxItem)cbCBC.SelectedItem).Content.ToString();
            }
            if (cbObjectiveByContent.IsChecked == true)
            {
                dc.ObjectiveSetByContent = ((ComboBoxItem)cbOBC.SelectedItem).Content.ToString();
            }
            return dc;
        }

        public ConstrainedChoiceConsiderations handleConstrainedChoiceConsiderations()
        {
            ConstrainedChoiceConsiderations ccc = new ConstrainedChoiceConsiderations();
            if (cbPrevent.IsChecked == true)
            {
                ccc.PreventActivation = ((ComboBoxItem)cbPreventActivation.SelectedItem).Content.ToString();
            }
            if (cbConstraint.IsChecked == true)
            {
                ccc.ConstrainChoice = ((ComboBoxItem)cbConstraintChoice.SelectedItem).Content.ToString();
            }
            return ccc;
        }

        public RollupConsiderations handleRollupConsideration()
        {
            RollupConsiderations rc = new RollupConsiderations();
            if (cbRFS.IsChecked == true)
            {
                rc.RequiredForSatisfied = ((ComboBoxItem)cbRequireForSatisfied.SelectedItem).Content.ToString();
            }
            if (cbRFNS.IsChecked == true)
            {
                rc.RequiredForNotSatisfied = ((ComboBoxItem)cbRequireForNotSatisfied.SelectedItem).Content.ToString();
            }
            if (cbRFC.IsChecked == true)
            {
                rc.RequiredForCompleted = ((ComboBoxItem)cbRequireForComplete.SelectedItem).Content.ToString();
            }
            if (cbRFIC.IsChecked == true)
            {
                rc.RequiredForIncomplete = ((ComboBoxItem)cbRequireForIncomplete.SelectedItem).Content.ToString();
            }
            if (cbMSIA.IsChecked == true)
            {
                rc.MeasureSatisfactionIfActive = ((ComboBoxItem)cbMeasureSatisfationIfActive.SelectedItem).Content.ToString();
            }
            return rc;
        }
        #endregion

        #region Enable UI Control

        private void cbUseCM_Checked(object sender, RoutedEventArgs e)
        {
            enableControlMode(true);
        }
        private void cbUseCM_UnChecked(object sender, RoutedEventArgs e)
        {
            enableControlMode(false);
        }

        private void cbUseLimit_Checked(object sender, RoutedEventArgs e)
        {
            enableLimitCondition(true);
        }
        private void cbUseLimit_UnChecked(object sender, RoutedEventArgs e)
        {
            enableLimitCondition(false);
        }

        private void cbUseRC_Checked(object sender, RoutedEventArgs e)
        {
            enableRandomizationControl(true);
        }
        private void cbUseRC_UnChecked(object sender, RoutedEventArgs e)
        {
            enableRandomizationControl(false);
        }

        private void cbUseDC_Checked(object sender, RoutedEventArgs e)
        {
            enableDeliveryControl(true);
        }
        private void cbUseDC_UnChecked(object sender, RoutedEventArgs e)
        {
            enableDeliveryControl(false);
        }

        private void cbUseConstraintChoice_Checked(object sender, RoutedEventArgs e)
        {
            enableConstraintChoice(true);
        }
        private void cbUseConstraintChoice_UnChecked(object sender, RoutedEventArgs e)
        {
            enableConstraintChoice(false);
        }

        private void cbUseRollupConsideration_Checked(object sender, RoutedEventArgs e)
        {
            enableRollupConsideration(true);
        }
        private void cbUseRollupConsideration_UnChecked(object sender, RoutedEventArgs e)
        {
            enableRollupConsideration(false);
        }

        private void useRollupRule_Checked(object sender, RoutedEventArgs e)
        {
            enableRollUpRule(true);
        }
        private void useRollupRule_UnChecked(object sender, RoutedEventArgs e)
        {
            enableRollUpRule(false);
        }

        private void usePreCond_Checked(object sender, RoutedEventArgs e)
        {
            enablePreConditionRule(true);
        }
        private void usePreCond_UnChecked(object sender, RoutedEventArgs e)
        {
            enablePreConditionRule(false);
        }

        private void usePostCond_Checked(object sender, RoutedEventArgs e)
        {
            enablePostConditionRule(true);
        }
        private void usePostCond_UnChecked(object sender, RoutedEventArgs e)
        {
            enablePostConditionRule(false);
        }

        private void useExitCond_Checked(object sender, RoutedEventArgs e)
        {
            enableExitConditionRule(true);
        }
        private void useExitCond_UnChecked(object sender, RoutedEventArgs e)
        {
            enableExitConditionRule(false);
        }

        private void enablePreConditionRule(bool enable)
        {
            listPreconditionGrid.IsEnabled = enable;
            AddRemovePreSection.IsEnabled = enable;
        }
        private void enablePostConditionRule(bool enable)
        {
            listPostConditionGrid.IsEnabled = enable;
            AddRemovePostSection.IsEnabled = enable;
        }
        private void enableExitConditionRule(bool enable)
        {
            listExitConditionGrid.IsEnabled = enable;
            AddRemovePostSecion.IsEnabled = enable;
        }
        private void enableRollUpRule(bool enable)
        {
            RollupRulleInitSection.IsEnabled = enable;
            RollupConfigSection.IsEnabled = enable;
            listRollupRuleGrid.IsEnabled = enable;
            AddRemoveRollupSection.IsEnabled = enable;
        }
        private void enableControlMode(bool enable)
        {
            cbUseCmDf.IsEnabled = enable;
            cbUseCmCus.IsEnabled = enable;
            cbUseChoice.IsEnabled = enable;
            cbUseChoiceExit.IsEnabled = enable;
            cbUseFlow.IsEnabled = enable;
            cbUseForward.IsEnabled = enable;
            cbChoice.IsEnabled = enable;
            cbChoiceExit.IsEnabled = enable;
            cbFlow.IsEnabled = enable;
            cbForward.IsEnabled = enable;
        }
        private void enableLimitCondition(bool enable)
        {
            cbAttemptLimit.IsEnabled = enable;
            cbAttemptDuration.IsEnabled = enable;
            txtAttemptLimit.IsEnabled = enable;
            txtAttemptDuration.IsEnabled = enable;
        }
        private void enableRandomizationControl(bool enable)
        {
            cbRandomTiming.IsEnabled = enable;
            cbSelectCount.IsEnabled = enable;
            cbReorder.IsEnabled = enable;
            cbSelectTiming.IsEnabled = enable;
            cbRandomizationTiming.IsEnabled = enable;
            txtSelectCount.IsEnabled = enable;
            cbReorderChildren.IsEnabled = enable;
            cbSelectionTiming.IsEnabled = enable;
        }
        private void enableDeliveryControl(bool enable)
        {
            cbUseTrack.IsEnabled = enable;
            cbCompleteByContent.IsEnabled = enable;
            cbObjectiveByContent.IsEnabled = enable;
            cbTrack.IsEnabled = enable;
            cbCBC.IsEnabled = enable;
            cbOBC.IsEnabled = enable;
        }
        private void enableConstraintChoice(bool enable)
        {
            cbPrevent.IsEnabled = enable;
            cbConstraint.IsEnabled = enable;
            cbPreventActivation.IsEnabled = enable;
            cbConstraintChoice.IsEnabled = enable;
        }
        private void enableRollupConsideration(bool enable)
        {
            cbRFS.IsEnabled = enable;
            cbRFNS.IsEnabled = enable;
            cbRFC.IsEnabled = enable;
            cbRFIC.IsEnabled = enable;
            cbMSIA.IsEnabled = enable;
            cbRequireForSatisfied.IsEnabled = enable;
            cbRequireForNotSatisfied.IsEnabled = enable;
            cbRequireForComplete.IsEnabled = enable;
            cbRequireForIncomplete.IsEnabled = enable;
            cbMeasureSatisfationIfActive.IsEnabled = enable;
        }

        #endregion

        #region Choose Action
        private void cbPreAction_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listPreconditionGrid.SelectedIndex >= 0)
            {
                listPreConditionData[listPreconditionGrid.SelectedIndex].ConditionAction =  cbPreAction.SelectedValue.ToString();
            }
            else
            {
                // MessageBox.Show("Please select a precodition rule to set action");
            }
        }

        private void cbPostAction_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listPostConditionGrid.SelectedIndex >= 0)
            {
                listPostConditionData[listPostConditionGrid.SelectedIndex].ConditionAction = cbPostAction.SelectedValue.ToString();
            }
            else
            {
                // MessageBox.Show("Please select a post codition rule to set action");
            }
        }

        private void cbExitAction_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listPostConditionGrid.SelectedIndex >= 0)
            {
                listPostConditionData[listPostConditionGrid.SelectedIndex].ConditionAction = cbExitAction.SelectedValue.ToString();
            }
            else
            {
                // MessageBox.Show("Please select a exit condition rule to set action");
            }
        }

        private void cbRollupAction_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listRollupRuleGrid.SelectedIndex > 0)
            {
                listRollupConditionData[listRollupRuleGrid.SelectedIndex].ConditionAction = cbRollupAction.SelectedValue.ToString();
            }
            else
            {
                // MessageBox.Show("Please select a rollup condition rule to set action");
            }
        }

        #endregion

        #region Handle Condition
        private List<PreConditionRule> handlePreConditionRule()
        {
            List<PreConditionRule> listPreConditonRule = new List<PreConditionRule>();
            for (int i = 0; i < listPreConditionData.Count; i++)
            {
                ConditionView conditionView = listPreConditionData[i];
                PreConditionRule pre = new PreConditionRule();
                pre.RuleAction = new RuleAction();
                pre.RuleAction.Action = conditionView.ConditionAction;
                RuleConditions rc = new RuleConditions();
                rc.ConditionCombination = conditionView.ConditionMode;
                rc.ListRuleCondition = new List<RuleCondition>();
                for (int j = 0; j < conditionView.ListConditionAction.Count; j++)
                {
                    ConditionActionView conditionActionView = conditionView.ListConditionAction[j];
                    RuleCondition ruleCondition = new RuleCondition();
                    ruleCondition.Condition = conditionActionView.Condition;
                    ruleCondition.Operator = conditionActionView.Operator;
                    rc.ListRuleCondition.Add(ruleCondition);
                }
                pre.RuleConditions = rc;
                listPreConditonRule.Add(pre);
            }
            return listPreConditonRule;
        }

        private List<PostConditionRule> handlePostConditionRule()
        {
            List<PostConditionRule> listPostConditonRule = new List<PostConditionRule>();
            for (int i = 0; i < listPostConditionData.Count; i++)
            {
                ConditionView conditionView = listPostConditionData[i];
                PostConditionRule post = new PostConditionRule();
                post.RuleAction = new RuleAction();
                post.RuleAction.Action = conditionView.ConditionAction;
                RuleConditions rc = new RuleConditions();
                rc.ConditionCombination = conditionView.ConditionMode;
                rc.ListRuleCondition = new List<RuleCondition>();
                for (int j = 0; j < conditionView.ListConditionAction.Count; j++)
                {
                    ConditionActionView conditionActionView = conditionView.ListConditionAction[j];
                    RuleCondition ruleCondition = new RuleCondition();
                    ruleCondition.Condition = conditionActionView.Condition;
                    ruleCondition.Operator = conditionActionView.Operator;
                    rc.ListRuleCondition.Add(ruleCondition);
                }
                post.RuleConditions = rc;
                listPostConditonRule.Add(post);
            }
            return listPostConditonRule;
        }

        private List<ExitConditionRule> handleExitConditionRule()
        {
            List<ExitConditionRule> listExitConditonRule = new List<ExitConditionRule>();
            for (int i = 0; i < listExitConditionData.Count; i++)
            {
                ConditionView conditionView = listExitConditionData[i];
                ExitConditionRule exit = new ExitConditionRule();
                exit.RuleAction = new RuleAction();
                exit.RuleAction.Action = conditionView.ConditionAction;
                RuleConditions rc = new RuleConditions();
                rc.ConditionCombination = conditionView.ConditionMode;
                rc.ListRuleCondition = new List<RuleCondition>();
                for (int j = 0; j < conditionView.ListConditionAction.Count; j++)
                {
                    ConditionActionView conditionActionView = conditionView.ListConditionAction[j];
                    RuleCondition ruleCondition = new RuleCondition();
                    ruleCondition.Condition = conditionActionView.Condition;
                    ruleCondition.Operator = conditionActionView.Operator;
                    rc.ListRuleCondition.Add(ruleCondition);
                }
                exit.RuleConditions = rc;
                listExitConditonRule.Add(exit);
            }
            return listExitConditonRule;
        }

        private List<RollupRule> handleRollupRule()
        {
            List<RollupRule> listRollupRule = new List<RollupRule>();
            for (int i = 0; i < listRollupConditionData.Count; i++)
            {
                RollUpRuleView rollupRuleView = listRollupConditionData[i];
                RollupRule rollupRule = new RollupRule();
                rollupRule.RollupAction = new RollupAction();
                rollupRule.RollupAction.Action = rollupRuleView.ConditionAction;
                if (rollupRuleView.ChildActivitySet.Length > 0)
                {
                    rollupRule.ChildActivitySet = rollupRuleView.ChildActivitySet;
                }
                if (rollupRuleView.MinimumCount.Length > 0)
                {
                    rollupRule.MinimumCount = rollupRuleView.MinimumCount;
                }
                if (rollupRuleView.MinimumPercent.Length > 0)
                {
                    rollupRule.MinimumPercent = rollupRuleView.MinimumPercent;
                }
                RollupConditions rc = new RollupConditions();
                rc.ConditionCombination = rollupRuleView.ConditionMode;
                rc.ListRollupCondition = new List<RollupCondition>();
                for (int j = 0; j < rollupRuleView.ListConditionAction.Count; j++)
                {
                    ConditionActionView conditionActionView = rollupRuleView.ListConditionAction[j];
                    RollupCondition ruleCondition = new RollupCondition();
                    ruleCondition.Condition = conditionActionView.Condition;
                    ruleCondition.Operator = conditionActionView.Operator;
                    rc.ListRollupCondition.Add(ruleCondition);
                }
                rollupRule.RollupConditions = rc;
                listRollupRule.Add(rollupRule);
            }
            return listRollupRule;
        }
        #endregion

        #region Handle Grid Selected Change
        private void listPreconditionGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (listPreconditionGrid.SelectedIndex >= 0)
                {
                    listPreConditionActionGrid.IsEnabled = true;
                    cbListPreCondition.IsEnabled = true;
                    cbPreAction.IsEnabled = true;
                    cbPreOperator.IsEnabled = true;
                    PreConditionActionControl.IsEnabled = true;
                    listPreConditionActionData = listPreConditionData[listPreconditionGrid.SelectedIndex].ListConditionAction;
                    //if (listPreConditionData[listPreconditionGrid.SelectedIndex].ConditionAction != null)
                    //    cbPreAction.SelectedValue = listPreConditionData[listPreconditionGrid.SelectedIndex].ConditionAction;
                    listPreConditionActionGrid.ItemsSource = listPreConditionActionData;
                }
                else
                {
                    listPreConditionActionGrid.IsEnabled = false;
                    cbListPreCondition.IsEnabled = false;
                    cbPreAction.IsEnabled = false;
                    PreConditionActionControl.IsEnabled = false;
                    cbPreOperator.IsEnabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void listPostConditionGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (listPostConditionGrid.SelectedIndex >= 0)
                {
                    listPostConditionActionGrid.IsEnabled = true;
                    cbListPostCondition.IsEnabled = true;
                    cbPostAction.IsEnabled = true;
                    cbPostOperator.IsEnabled = true;
                    PostConditionActionControl.IsEnabled = true;
                    if (listPostConditionData[listPostConditionGrid.SelectedIndex].ListConditionAction != null)
                    {
                        listPostConditionActionData = listPostConditionData[listPostConditionGrid.SelectedIndex].ListConditionAction;
                        listPostConditionActionGrid.ItemsSource = listPostConditionActionData;
                    }
                    //if (listPostConditionData[listPostConditionGrid.SelectedIndex].ConditionAction != null)
                    //    cbPostAction.SelectedValue = listPostConditionData[listPostConditionGrid.SelectedIndex].ConditionAction;

                }
                else
                {
                    listPostConditionActionGrid.IsEnabled = false;
                    cbListPostCondition.IsEnabled = false;
                    cbPostAction.IsEnabled = false;
                    cbPostOperator.IsEnabled = false;
                    PostConditionActionControl.IsEnabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void listExitConditionGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (listExitConditionGrid.SelectedIndex >= 0)
                {
                    listExitConditionActionGrid.IsEnabled = true;
                    cbListExitCondition.IsEnabled = true;
                    cbExitAction.IsEnabled = true;
                    cbExitOperator.IsEnabled = true;
                    ExitConditionActionControl.IsEnabled = true;
                    listExitConditionActionData = listExitConditionData[listExitConditionGrid.SelectedIndex].ListConditionAction;
                    //if (listExitConditionData[listExitConditionGrid.SelectedIndex].ConditionAction != null)
                    //    cbExitAction.SelectedValue = listExitConditionData[listExitConditionGrid.SelectedIndex].ConditionAction;
                    listExitConditionActionGrid.ItemsSource = listExitConditionActionData;
                }
                else
                {
                    listExitConditionActionGrid.IsEnabled = false;
                    cbListExitCondition.IsEnabled = false;
                    cbExitAction.IsEnabled = false;
                    cbExitOperator.IsEnabled = false;
                    ExitConditionActionControl.IsEnabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void listRollupRuleGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (listRollupRuleGrid.SelectedIndex >= 0)
                {
                    listRollupConditionActionGrid.IsEnabled = true;
                    cbListRollupCondition.IsEnabled = true;
                    cbRollupAction.IsEnabled = true;
                    cbRollupOperator.IsEnabled = true;
                    RollupConditionActionControl.IsEnabled = true;
                    if (listRollupConditionData[listRollupRuleGrid.SelectedIndex].ListConditionAction != null)
                    {
                        listRollupConditionActionData = listRollupConditionData[listRollupRuleGrid.SelectedIndex].ListConditionAction;
                        listRollupConditionActionGrid.ItemsSource = listRollupConditionActionData;
                    }
                    //if (listRollupConditionData[listRollupRuleGrid.SelectedIndex].ConditionAction != null)
                    //    cbRollupAction.SelectedValue = listRollupConditionData[listRollupRuleGrid.SelectedIndex].ConditionAction;

                }
                else
                {
                    listRollupConditionActionGrid.IsEnabled = false;
                    cbListRollupCondition.IsEnabled = false;
                    cbRollupAction.IsEnabled = false;
                    cbRollupOperator.IsEnabled = false;
                    RollupConditionActionControl.IsEnabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

    }
}
